import Vue from 'vue'
import Vuex from 'vuex'
import request from '@/utils/request.js'
import {wxOauth} from '@/utils/weixin.js'
import {getUser,wxUrls,outLogins} from '@/utils/api.js'

Vue.use(Vuex)

const store = new Vuex.Store({
  state: {
		token: '',
		openId:'',  
		wechat_code:'',
		userInfos:'' 
	},
	mutations: {
		SET_TOKEN: (state, token) => {
			state.token = token
		},
		SET_USERINFO: (state, users) => {
			state.userInfos = users
		},
		SET_OPENID: (state, openId) => {
			state.openId = openId
		}, 
	},
	actions: { 
		//手机号登录
		async PhoneLogin({ commit,dispatch }, info) { 
			var _this = this;
			uni.request({
				url: `${request.BASE_API}/member/smsLogin`,
				data: info,
				method:'POST',
				success: function(res) {  
					console.log(res,"----------");
					if(res.data.code == '200'){ 
						dispatch("getUser");  //获取用户信息
						// #ifdef MP-WEIXIN
						// dispatch("getOpenId");  //单独获取openid 小程序
						commit('SET_OPENID', res.data.result.openId)    
						uni.setStorageSync('OPENID',res.data.result.openId); 
						// #endif
						
						// #ifdef H5 
						// dispatch("wxOauth") 
						// dispatch("openId",wxOauth());  //获取openid 公众号
						// #endif
						uni.setStorageSync('TOKEN',res.data.result.token);
						commit('SET_TOKEN', res.data.result.token) 
						uni.showToast({
							title:'登录成功',
							icon:'success'
						}) 
						setTimeout(()=>{
							uni.switchTab({
								url:'/pages/index/index'
							})  
						},1000)  
					}else{ 
						uni.showToast({
							title:res.data.message,
							icon:'none'
						})
					}
				},  
			}); 
		},
		//小程序获取手机号一键登录
		async weixinMiniLogin({ commit,dispatch }, info,code) { 
			console.log(code); 
			var _this = this;
			uni.request({
				url: `${request.BASE_API}/member/weChatMini`, //小程序一键登录
				data: info,
				method:'POST',
				success: function(res) {  
					if(res.data.code == '200'){ 
						dispatch("getUser");  //获取用户信息 
						  
						// #ifdef MP-WEIXIN
						// dispatch("openId",code);
						dispatch("getOpenId");  //获取openid 小程序
						// #endif
						uni.setStorageSync('TOKEN',res.data.result.token);
						commit('SET_TOKEN', res.data.result.token) 
						uni.showToast({
							title:'登录成功',
							icon:'success'
						})
						setTimeout(()=>{
							uni.switchTab({
								url:'/pages/index/index'
							}) 
						},1000) 
					}else{  
						uni.showToast({
							title:res.data.message,
							icon:'none'
						})
					}
				},  
			}); 
		},
		//公众号绑定手机号
		async bindPhone({ commit,dispatch }, info) { 
			var _this = this;
			uni.request({
				url: `${request.BASE_API}/member/officialAccounts`, //公众号授权登录
				data: info,
				method:'POST',
				success: function(res) {  
					// alert(JSON.stringify(res.data))
					if(res.data.code == '200'){ 
						dispatch("getUser");  //获取用户信息
						// #ifdef MP-WEIXIN
						dispatch("getOpenId");  //获取openid 小程序
						// #endif
						
						// #ifdef H5 
						// dispatch("wxOauth") 
						// dispatch("openId",wxOauth());  //获取openid 公众号
						// #endif
						uni.setStorageSync('TOKEN',res.data.result.token);
						commit('SET_TOKEN', res.data.result.token) 
						uni.showToast({
							title:'登录成功',
							icon:'success'
						}) 
						setTimeout(()=>{
							uni.switchTab({
								url:'/pages/index/index'
							})  
						},1000)  
					}else{ 
						uni.showToast({
							title:res.data.message,
							icon:'none'
						})
					} 
				},  
			}); 
		},
		// 获取用户信息
		getUser({commit}){
			getUser().then((res) => {
				console.log(res,"用户信息");  
				if(res.code == '200'){ 
					var users = res.result 
					uni.setStorageSync('USERINFO', users) 
					commit('SET_USERINFO', users)  
				}
			})  
		},
		// 小程序获取openid
		getOpenId({ dispatch }) {  
			console.log('小程序登录');
			uni.login({
				success: res => { 
					let code = res.code;   
					dispatch("openId",code);
				}
			});
		}, 
		// code 获取openid
		openId({ commit },code){ 
			console.log(code,typeof(code),"通过code获取openid"); 
			let url = `${request.BASE_API}/wx/miniGetOpenId?code=${code}`; 
			uni.request({
				url: url, // 请求路径
				data: { 
					// code:code
				}, 
				method: 'POST', //请求方法,
				header: {
					'Content-Type':'application/x-www-form-urlencoded',
					"Authorization":'Bearer ' + uni.getStorageSync('TOKEN'),
				}, 
				success: result => {    
					commit('SET_OPENID', result.data.result)
					uni.setStorageSync('OPENID',result.data.result); 
				},
				fail: err => {} //失败
			});
		},  
		// 登出
		Logout({ commit, state }) {
			return new Promise((resolve) => {   
				uni.removeStorageSync('USERINFO')
				uni.removeStorageSync('OPENID')
				uni.removeStorageSync('TOKEN');
				commit('SET_USERINFO', '') 
				commit('SET_TOKEN', '')  
				outLogins().then(() => { 
					uni.showToast({
						title:'退出登录',
						icon:'success'
					}) 
					//#ifdef MP-WEIXIN
					console.log('tiaozhuan'); 
						setTimeout(()=>{
							uni.switchTab({
								url:'/pages/index/index'
							})  
						},1000) 
					//#endif 
					//#ifdef H5
						// window.location.href= 'http://www.spadj.com'   //退出登录重定向
						setTimeout(()=>{
							window.location.href=`${process.env.UNI_BASE_URL}/user`
						},1000) 
						
					//#endif 
					uni.clearStorageSync(); 
					
				}).catch(() => {
					
				})
			})
		},

	},
	getters:{
		token: state => state.token, 
		username: state => {state.userid=uni.getStorageSync(USER_INFO).username; return state.username},
		nickname: state => {state.userid=uni.getStorageSync(USER_INFO).realname; return state.user.realname},
		avatar: state => {state.userid=uni.getStorageSync(USER_INFO).avatar; return state.user.avatar},
		userid:state => {state.userid=uni.getStorageSync(USER_INFO).id; return state.userid},
	}
})

export default store
